package com.qriver.cloud.consumer;

import com.sun.istack.internal.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 测试负载均衡服务
 */
@RestController
public class LoadBalancerController {

    Logger logger = Logger.getLogger(LoadBalancerController.class);

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("/loadBalancer")
    public String loadBalancer() {
        logger.info("执行消费者LoadBalancerController的loadBalancer()方法！");
        ServiceInstance instance = loadBalancerClient.choose("eureka-provider");
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/provider";
        return url;
    }
}
